package org.gaaidou.ptarmigan.springboot.core.serialize.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.extern.slf4j.Slf4j;
import org.gaaidou.ptarmigan.springboot.core.common.Common;

import java.io.IOException;
import java.io.Serial;
import java.lang.reflect.InvocationTargetException;


class DefaultDataSerializer extends StdSerializer<String> {

    protected DefaultDataSerializer(Class<String> t) {
        super(t);
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // nothing to do
    }
}

@Slf4j
class DesensitizationDataSerializer extends StdSerializer<String> {
    @Serial
    private static final long serialVersionUID = 1L;

    private Desensitize desensitize;

    public DesensitizationDataSerializer() {
        super(String.class);
    }

    public void setDesensitize(Desensitize desensitize) {
        this.desensitize = desensitize;
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        try {
            Desensitization desensitization = (Desensitization)desensitize.desensitization().getConstructors()[0].newInstance();
            gen.writeString(desensitization.desensitize(value));
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            log.warn("todo");
        }
    }
}

class IgnoreDataSerializer extends StdSerializer<String> {
    @Serial
    private static final long serialVersionUID = 1L;

    public IgnoreDataSerializer() {
        super(String.class);
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeString(Common.NONE_STRING);
    }
}
